import D2Ext

from .CImage import CImage
from .Constants import *


class CEditBox(CImage):

    def __init__(self, parent=None):
        super(CEditBox, self).__init__(parent)
        self.connect(self.SIGNAL_ONCLICK, self.on_click)
        self.TextPos = 0
        self.CursorPos = 0
        self.checked = False
        self.Text = ""
        self.TextX = 0
        self.TextY = 0
        self.is_focused = False
        self.TextMax = 20

    def set_text(self, text):
        self.Text = text

    def set_text_offset(self, x, y):
        self.TextX = x
        self.TextY = y

    def show(self):
        super(CEditBox, self).show()

    def hide(self):
        super(CEditBox, self).hide()

    def on_click(self):
        self.checked = not self.checked

    def draw(self):
        if self.image_file:
            D2Ext.Image_Draw(self.image_file, self.dx, self.dy + self.height, self.opacity)
        D2Ext.View_PrintString(self.Text, self.dx + self.TextX, self.dy - self.TextY, self.color, self.opacity)
        if D2Ext.View_GetTickCount() % 10 == 0:
            text_width = D2Ext.View_GetPixelLen(self.Text[:self.CursorPos] + '2')
            D2Ext.View_DrawLine(text_width + self.dx + self.TextX, self.dy - self.TextY, text_width + self.dx + self.TextX + 5, self.dy - self.TextY, 255, 0)

    def event(self, sender, wparam):
        if sender == WM_LBUTTONDOWN:
            if self.dx <= D2Ext.View_GetMouseX() <= self.dx + self.width and self.dy <= D2Ext.View_GetMouseY() <= self.dy + self.height:
                self.being_pressed = True
                return True
        elif sender == WM_LBUTTONUP:
            # if (cState == DISABLED) return false;
            if self.dx <= D2Ext.View_GetMouseX() <= self.dx + self.width and self.dy <= D2Ext.View_GetMouseY() <= self.dy + self.height:
                if self.state == VISIBLE:
                    self.is_focused = True
                    D2Ext.LogMsg("Focused")
                self.being_pressed = False
                return True
            else:
                if self.state == VISIBLE:
                    self.is_focused = False
        elif sender == WM_KEYDOWN:
            if not self.is_focused:
                return False
            D2Ext.LogMsg("KeyDown")
            shift_state = ((D2Ext.View_GetKeyState(VK_LSHIFT) & 0x80) or (D2Ext.View_GetKeyState(VK_RSHIFT) & 0x80))
            OldText = self.Text
            if wparam == VK_BACK:
                if self.CursorPos > 0:
                    self.Text = self.Text[:self.CursorPos - 1] + self.Text[self.CursorPos:]
                    # self.Text.erase(self.CursorPos - 1, 1)
                    self.CursorPos -= 1
                    if D2Ext.View_GetPixelLen(self.Text) > (self.width - (self.TextX * 2) - 5):
                        self.TextPos -= 1
                    else:
                        self.TextPos = 0
            elif wparam == VK_DELETE:
                if len(self.Text):
                    self.Text = self.Text[:self.CursorPos] + self.Text[self.CursorPos + 1:]
                    # self.Text.erase(self.CursorPos, 1)
            elif wparam == VK_LEFT:
                if self.CursorPos > 0:
                    self.CursorPos -= 1
                if D2Ext.View_GetPixelLen(self.Text) > (self.width - (self.TextX * 2) - 5):
                    self.TextPos -= 1
                else:
                    self.TextPos = 0

            elif wparam == VK_RIGHT:
                if self.CursorPos < len(self.Text):
                    self.CursorPos += 1

                if D2Ext.View_GetPixelLen(self.Text[self.TextPos:]) > (self.width - (self.TextX * 2) - 5) and self.TextPos < len(self.Text):
                    self.TextPos += 1
            elif wparam == 0x13:
                self.is_focused = False
            elif wparam == VK_RETURN:
                self.is_focused = False
            else:
                if len(self.Text) != self.TextMax:
                    key = wparam
                    tk = chr(int(key))
                    D2Ext.LogMsg("self.CursorPos=%s, key=%s" % (self.CursorPos, key))
                    if tk.isdigit() or tk.isspace():
                        if not shift_state:
                            key = str(key).lower()
                        lt = list(self.Text)

                        lt.insert(self.CursorPos, chr(int(key)))
                        self.Text = ''.join(lt)
                        # self.Text.insert(self.CursorPos, 1, key)
                        self.CursorPos += 1
                        if D2Ext.View_GetPixelLen(self.Text) > (self.width - (self.TextX * 2) - 5):
                            self.TextPos += 1

            # if OldText != self.Text and event_onChange:
            #     event_onChange(this)
            return True

        return False
